#include"headfile.h"


int del(void)
{
    FILE * fpStu = fopen("./document/student.txt","r");
    FILE * fpUser = fopen("./document/user.txt","r");
    if(NULL == fpStu && NULL == fpUser)
    {
        printf("打开文件失败:%m\n");
        return -1;
    }
    memNode * pStu = _getList(fpStu, stu_num);
    memNode * pUser = _getList(fpUser, user_num);
    fclose(fpStu);
    fclose(fpUser);

    if(NULL == pStu && NULL == pUser)
    {
        printf("没有学生存在,无法进行删除！\n");
        return -1;
    }

    int id;
    char is,c;
    do
    {
        printf("请输入您要删除的学生的学号 输-1返回:");
delete: while(0 == scanf("%d",&id))
        {
            printf("您输入的学号有误,请重新进行输入:");
            while(getchar() != '\n');
        }
        getchar();
        if(-1 == id) return 0; //输入-1则返回

        memNode * stuDel = findNode(pStu, id); //获取要删除的节点
        if(NULL == stuDel)
        {
            printf("该学生不存在,请重新输入 输-1返回:");
            goto delete;
        }
        
        memNode * userDel = findNode(pUser, id); //获取要删除的节点

        printf("是否要删除%s y/n:",name_delete);
        while(1 == scanf("%c",&is)) //异常处理
        {
            if((is == 'y' || is == 'n') && (c = getchar()) == '\n') 
                break;
            else
            {
                while((c = getchar()) != '\n');
                printf("您输入的操作有误,请重新输入:");
            }
        }
        if('y' == is)
        {
            _delNode(&pStu, stuDel);
            _delNode(&pUser, userDel);
        }
        else if('n' == is)
        {
            printf("请再次您要删除的学生的学号 输-1返回:");
            goto delete;
        }

        printf("是否要继续删除其他学生 y/n:");
        while(1 == scanf("%c",&is)) //异常处理
        {
            if((is == 'y' || is == 'n') && (c = getchar()) == '\n') 
                break;
            else
            {
                while((c = getchar()) != '\n');
                printf("您输入的操作有误,请重新输入:");
            }
        }
    }while(is == 'y');

    fpStu = fopen("./document/student.txt","w");
    memNode * pTemp = pStu;
    while(pStu) //要释放整个链表 不然会内存泄漏
    {
        fprintf(fpStu,"%20s%20s%20d%20d\n",pStu->mem.name,pStu->mem.sex,pStu->mem.age,pStu->mem.id);
        pTemp = pStu->next;
        free(pStu);
        pStu = pTemp;
    }
    fclose(fpStu);

    fpUser = fopen("./document/user.txt","w");
    pTemp = pUser;
    while(pUser) //要释放整个链表 不然会内存泄漏
    {
        fprintf(fpUser,"%20d%20s%20s\n",pUser->mem.id,pUser->mem.name,pUser->mem.password);
        pTemp = pUser->next;
        free(pUser);
        pUser = pTemp;
    }
    fclose(fpUser);
    return 0;
}

void _delNode(memNode* * p, memNode * pDel)
{
    if(NULL == p || NULL == pDel) return;

    if(pDel == *p) //要删的是第一个节点
            *p = pDel->next;
    else
    {
        memNode * pTemp = *p;
        while(pTemp->next)
        {
            if(pTemp->next == pDel) break;
            pTemp = pTemp->next;
        }
        pTemp->next = pDel->next;
    }
    free(pDel); //释放
    pDel = NULL;
}

memNode* _getList(FILE * fp, int num)
{
    member mem = {0,0,0,0,0};
    memNode * p = NULL;
    if(stu_num == num)
        while(4 == fscanf(fp,"%s%s%d%d",mem.name,mem.sex,&mem.age,&mem.id))
            tailAdd(&p,getNode(mem,stu_num));
    else if(user_num == num)
        while(3 == fscanf(fp,"%d%s%s",&mem.id,mem.name,mem.password))
            tailAdd(&p,getNode(mem,user_num));
    return p;
}